/* 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Title : com.Angaliwa.Settings
Author : Dave Jansen -- Zenitum
URL : http://www.zenitum.com/

Description : This module takes care of all daisyChain 
			  actions for the other Angaliwa modules.

History		: v1.2.090828 Re-added parent object to
			  keep the module consistent with the
			  rest of Angaliwa. The value is now,
			  however, optional, and the module
			  will return itself if no parent is
			  provided.
			  
			  v1.1.090826 Parent object no longer
			  required, as each method now returns
			  the settings object, not it's parent.
			  
			  v1.0.090727 Initial version

Created 	: July 27th, 2009

Version		: 1.1
Compatibility: 1.1

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
*/

package com.Angaliwa{
	public class Settings{
		private var parentObject:*,
					container:Object = [],
					defaults:Object = [];
		
		/*
		 * Summary:      	Sets up default values, if needed.
		 * Return:       	* (parentObject)
		 * Version:   	 	1.3
		 * Compatibility:	1.0
		 */
		public function Settings(parent:*=undefined){
			this.parentObject = parent;
		}
		
		/*
		 * Summary:      	Sets up default values, if needed.
		 * Return:       	Settings (this)
		 * Version:   	 	1.1
		 * Compatibility:	1.1
		 */
		public function setDefaults(settings:Object):*{
			if(!defaults)
				defaults = settings;
			else
				for(var s:String in settings)
					defaults[s] = settings[s];
			
			return (parentObject) ? parentObject : this;
		}
		
		/*
		 * Summary:      	Checks if specified setting exists and returns
		 					it if true. If false, checks if it exists in
		 					defaults and returns that. Returns undefined
		 					if all else fails.
		 * Return:       	*
		 * Version:   	 	1.1
		 * Compatibility:	1.0
		 */
		public function get(name:String=undefined):*{
			if(container.hasOwnProperty(name))
				return container[name];
			else if(defaults[name] != undefined)
				return defaults[name];
			
			return undefined;
		}
		
		/*
		 * Summary:      	Adds a colection of settings to the settings 
		 					list.
		 * Return:       	Settings (this)
		 * Version:   	 	1.1
		 * Compatibility:	1.1
		 */
		public function add(settings:Object):*{
			for(var s:String in settings)
				container[s] = settings[s];
			return (parentObject) ? parentObject : this;
		}
		
		/*
		 * Summary:      	Removes a setting from the settings list.
		 * Return:       	Settings (this).
		 * Version:   	 	1.1
		 * Compatibility:	1.0
		 */
		public function del(name:String):*{
			if(container[name] != undefined)
				delete container[name];
			return (parentObject) ? parentObject : this;
		}
		
		/*
		 * Summary:      	Compares all settings with the object provided,
		 					and returns true if they all compare.
		 * Return:       	Boolean
		 * Version:   	 	1.0
		 * Compatibility:	1.0
		 */
		public function compare(second:Object):Boolean{
			for(var c:String in (second as Object))
				if(get(c) != second[c]) return false;
			
			return true;
		}
	}
}